package com.crm.business.crm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.crm.business.crm.mapper.HpPaymentTypeDictMapper;
import com.crm.business.crm.mapper.BillPaymentDeptBindMapper;
import com.crm.business.crm.service.BillPaymentDeptBindService;
import com.crm.common.crm.constant.CrmCodeEnum;
import com.crm.common.crm.entity.PO.HpPaymentTypeDict;
import com.crm.common.crm.entity.PO.BillPaymentDeptBind;
import com.crm.common.crm.entity.VO.PaymentDeptBindVO;
import com.crm.common.ka.core.exception.CrmException;
import com.crm.business.servlet.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BillPaymentDeptBindServiceImpl extends BaseServiceImpl<BillPaymentDeptBindMapper, BillPaymentDeptBind> implements BillPaymentDeptBindService {


    @Autowired
    private HpPaymentTypeDictMapper hpPaymentTypeDictMapper;

    /**
     * 付款方式绑定部门回显
     * @param deptId
     * @return
     */
    @Override
    public List<PaymentDeptBindVO> paymentDeptBindList(Integer deptId){
        List<PaymentDeptBindVO> voList = new ArrayList<>();
        List<HpPaymentTypeDict> dictList =  hpPaymentTypeDictMapper.queryAll();
        if(CollectionUtil.isNotEmpty(dictList)){
            List<HpPaymentTypeDict> threeList = dictList.stream().filter(dict-> String.valueOf(3).equals(dict.getPaymentLevel())).collect(Collectors.toList());

            Map<Integer,HpPaymentTypeDict> map = dictList.stream().collect(Collectors.toMap(HpPaymentTypeDict::getId, v -> v, (p1, p2) -> p1));

            for(HpPaymentTypeDict paymentTypeDict: threeList){
                PaymentDeptBindVO vo = new PaymentDeptBindVO();
                vo.setThirdId(paymentTypeDict.getId());
                String paymentDict = paymentTypeDict.getPaymentName();

                HpPaymentTypeDict twoDict = map.get(paymentTypeDict.getParentId());
                if(Optional.ofNullable(twoDict).isPresent()){
                    vo.setSecondaryId(twoDict.getId());
                    paymentDict = twoDict.getPaymentName()+ "|" +paymentDict;

                    HpPaymentTypeDict oneDict = map.get(twoDict.getParentId());
                    if(Optional.ofNullable(oneDict).isPresent()){
                        vo.setFirstId(oneDict.getId());
                        paymentDict = oneDict.getPaymentName()+ "|" +paymentDict;
                    }
                }

                vo.setPaymentDict(paymentDict);
                if(Optional.ofNullable(vo.getFirstId()).isPresent()
                        && Optional.ofNullable(vo.getSecondaryId()).isPresent()
                        && Optional.ofNullable(vo.getThirdId()).isPresent()){
                    voList.add(vo);
                }
            }
        }

        List<BillPaymentDeptBind> list =  this.baseMapper.queryByDeptId(deptId);
        if(CollectionUtil.isNotEmpty(list)){
            List<Integer> ids =  list.stream().map(BillPaymentDeptBind::getPaymentId).collect(Collectors.toList());
            for(PaymentDeptBindVO vo:voList){
                if(ids.contains(vo.getThirdId())){
                    vo.setCheck(true);
                }
            }
        }

        return voList;
    }

    /**
     * 付款方式与部门绑定
     * @param list
     * @return
     */
    @Override
    public Integer paymentDeptBind(Integer deptId, List<Integer> list) {
        if(!Optional.ofNullable(deptId).isPresent() || CollectionUtil.isEmpty(list)){
            throw new CrmException(CrmCodeEnum.CRM_RECEIVABLES_PAYMENT_DICT_BIND);
        }
        LambdaQueryWrapper<BillPaymentDeptBind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BillPaymentDeptBind::getDeptId, deptId);
        this.baseMapper.delete(wrapper);
        for(Integer paymentId :list){
            BillPaymentDeptBind bind = new BillPaymentDeptBind();
            bind.setPaymentId(paymentId);
            bind.setDeptId(deptId);
            baseMapper.insert(bind);
        }
        return 1;
    }

    @Override
    public List<BillPaymentDeptBind> queryByDeptId(Integer deptId){
        return  this.baseMapper.queryByDeptId(deptId);
    }


}
